using System.Collections;
using System.Collections.Generic;
using UnityEngine.EventSystems;
using UnityEngine;
using TMPro;

public class EnergyUI : MonoBehaviour
{
    [Header("系统引用")]
    public PlayerData playerData;

    [Header("组件引用")]
    public TMP_Text energyText;

    
    public bool _isInitialized = false;

    void Start()
    {
        InitializeEnergyText();
    }

    private void InitializeEnergyText()
    {
        if (_isInitialized) return;

        // 自动查找缺失的引用
        if (playerData == null)
        {
            playerData = FindObjectOfType<PlayerData>();
            if (playerData == null)
            {
                Debug.LogError("HandView: 未找到PlayerData！");
                return;
            }
        }

        // 订阅事件
        playerData.OnEnergyChanged += SetEnergyText;
        _isInitialized = true;
   

        SetEnergyText(playerData.maxEnergy);
        
    }

    private void SetEnergyText(int energy)
    {
        energyText.text = energy.ToString();
    }
}
